package com.relax.netty.tcp.server;


import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.AttributeKey;


/**
 * @author 彭青松
 */
public class MyServer {

    public static void main(String[] args) {
        // 1.构建 bossGroup 和 workGroup
        NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
        bossGroup.setIoRatio(20);
        EventLoopGroup workGroup = new NioEventLoopGroup(2);

        ServerBootstrap serverBootstrap = new ServerBootstrap();
        // 2.设置group
        serverBootstrap.group(bossGroup, workGroup);
        // 3.设置服务端option和attr
        serverBootstrap.option(ChannelOption.SO_BACKLOG, 1);

        serverBootstrap.attr(AttributeKey.valueOf("name"), "彭青松");

//        chang.getattt(name)
        // 4.设置服务端的channel
        serverBootstrap.channel(NioServerSocketChannel.class);
        // 5.设置服务端的handler
        serverBootstrap.handler(new MyServerHandler());


        // 6.设置客户端的handler
        serverBootstrap.childHandler(new MyTcpServerInitializer());

        try {
            // 8.服务绑定端口,启动
            ChannelFuture channelFuture = serverBootstrap.bind(8888).sync();
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }
    }
}
